package com.hl.card.modular.pack.controller;


import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hl.card.common.persistence.model.TextAddressType;
import com.hl.card.common.persistence.model.TextUser;
import com.hl.card.common.util.ResponseData;
import com.hl.card.modular.pack.service.TextAddressService;

@RestController
@RequestMapping("/api/textAddress")
public class TextAddressController {
	@Autowired
	private TextAddressService textAddressService;
	@Autowired
	private ResponseData responseData;
	
	@RequestMapping("/getTextUserByUserId")
	public ResponseData getTextUserByUserId(String user_id,int currentPage,int pageSize) {
		if(StringUtils.isEmpty(user_id))
		{
			responseData.setData(null);
			responseData.setMsg("用户ID不能为空");
			responseData.setStatus("0");
			return responseData;
		}
		TextUser textUser=textAddressService.getTextUserByUserId(user_id,currentPage,pageSize);
		responseData.setData(textUser);
		responseData.setMsg("操作成功");
		responseData.setStatus("1");
		return responseData;
	}

	@RequestMapping("/deleteAddressText")
	public ResponseData deleteAddressText(String user_id, String address)
	{
		if(StringUtils.isEmpty(user_id))
		{
			responseData.setData(null);
			responseData.setMsg("用户ID不能为空");
			responseData.setStatus("0");
			return responseData;
		}
		if(StringUtils.isEmpty(address))
		{
			responseData.setData(null);
			responseData.setMsg("地址不能为空");
			responseData.setStatus("0");
			return responseData;
		}
		textAddressService.deleteAddressText(user_id, address);
		responseData.setData(null);
		responseData.setMsg("操作成功");
		responseData.setStatus("1");
		return responseData;
	}
	
	@RequestMapping("/updateAddressText")
	public ResponseData updateAddressText(String user_id,String oldAddress, String address, String remark,String userName,String type)
	{
		if(StringUtils.isEmpty(user_id))
		{
			responseData.setData(null);
			responseData.setMsg("用户ID不能为空");
			responseData.setStatus("0");
			return responseData;
		}
		if(StringUtils.isEmpty(address))
		{
			responseData.setData(null);
			responseData.setMsg("地址不能为空");
			responseData.setStatus("0");
			return responseData;
		}
		if(StringUtils.isEmpty(type))
		{
			responseData.setData(null);
			responseData.setMsg("类型不能为空");
			responseData.setStatus("0");
			return responseData;
		}
		if(!oldAddress.equals(address))
		{
			boolean result = Pattern.matches("0x([0-9A-Za-z]{40})", address);
			if(!result)
			{
				responseData.setData(null);
				responseData.setMsg("地址格式错误");
				responseData.setStatus("0");
				return responseData;
			}
			long count=textAddressService.getCount(user_id,address);  //查询是否重复
			if(count>0)
			{
				responseData.setData(null);
				responseData.setMsg("地址已存在");
				responseData.setStatus("0");
				return responseData;
			}
		
		}
	
		textAddressService.updateAddressText(user_id,oldAddress, address, remark,userName,type);
		responseData.setData(null);
		responseData.setMsg("操作成功");
		responseData.setStatus("1");
		return responseData;
		
	}
	@RequestMapping("/insertAddressText")
	public ResponseData insertAddressText(String user_id, String address, String remark,String userName,String type)
	{
		if(StringUtils.isEmpty(user_id))
		{
			responseData.setData(null);
			responseData.setMsg("用户ID不能为空");
			responseData.setStatus("0");
			return responseData;
		}
		if(StringUtils.isEmpty(address))
		{
			responseData.setData(null);
			responseData.setMsg("地址不能为空");
			responseData.setStatus("0");
			return responseData;
		}
		if(StringUtils.isEmpty(type))
		{
			responseData.setData(null);
			responseData.setMsg("类型不能为空");
			responseData.setStatus("0");
			return responseData;
		}
		boolean result = Pattern.matches("0x([0-9A-Za-z]{40})", address);
		if(!result)
		{
			responseData.setData(null); 
			responseData.setMsg("地址格式错误");
			responseData.setStatus("0");
			return responseData;
		}
		long count=textAddressService.getCount(user_id,address);  //查询是否重复
		if(count>0)
		{
			responseData.setData(null);
			responseData.setMsg("地址已存在");
			responseData.setStatus("0");
			return responseData;
		}
		textAddressService.insertAddressText(user_id, address, remark,userName,type);
		responseData.setData(null);
		responseData.setMsg("操作成功");
		responseData.setStatus("1");
		return responseData;
	}
	
	@RequestMapping("/getTextAddressType")
	public ResponseData getTextAddressType()
	{
		List<TextAddressType>  list=textAddressService.getTextAddressType();
		responseData.setData(list);
		responseData.setMsg("操作成功");
		responseData.setStatus("1");
		return responseData;
	}
}
